// pages/defect/craft/params/index.js
import {
  PARAMS_TYPE
} from '../../../../dataSource';

Component({
  /**
   * 组件的属性列表
   */
  properties: {
    products: {
      type: Array,
      value: [] 
    },
    dataSource: {
      type: Array,
      value: [],
      observer(data){
        const datas = this.properties.products.filter(it=>{
          return data.find(item=>{
             return item.finishProductSettingsId === it.id
          }) ? false : true
        }).map(it=>{
            return {
              name: it.name,
              value: it.id 
            }
          })
       this.setData({
            productList: datas
        })
      }
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    _info: {},
    name: '',
    vals: [],
    productList: [],
 
    paramsType: PARAMS_TYPE
  },

  /**
   * 组件的方法列表
   */
  methods: {
    onCancel() {
       this.onClear();
      this.triggerEvent('cancel');
    },
    onSubmit(event) {
 
      const {
        ballGrindingFineness,
        glazeFlowRate,
        glazeRatio,
        glazingAmount
      } = event.detail.value;
      if (ballGrindingFineness &&
        glazeFlowRate &&
        glazeRatio &&
        glazingAmount &&
        this.data.vals.length > 0) {
        if (this.data._info.id) {
          this.triggerEvent('update', {
            finishProductSettingsId: this.data.vals[0],
            ...event.detail.value,
            adjustmentGlazeTypeId: this.data._info.adjustmentGlazeTypeId,
            id: this.data._info.id
          });
        } else {
          this.triggerEvent('submit', {
            finishProductSettingsId: this.data.vals[0],
            ...event.detail.value
          });
        }
        this.onClear();
      } else {
        wx.showToast({
          title: '未按要求填写表单',
          icon: 'none'
        })
      }
    },
    initData(data) {
      this.setData({
        _info: data
      });
     
      if(data?.id){
        const item = this.properties.products.find(it=> it.id === data.finishProductSettingsId);
        this.setData({
             productList: [{
               name: item.name,
               value: item.id
             }, ...this.data.productList]
        })
      setTimeout(() => {
         this.setData({
            vals: [data.finishProductSettingsId] 
        })
       }, 100);
      }
    },
    onSelect(event) {
      this.setData({
          vals: event.detail
      })
 
    },
 
    onInput(event) {
      const value = event.detail,
        key = event.currentTarget.dataset.key;
      const form = this.data.form;
      form[key] = value;
      this.setData({
        form
      })
    },
    onClear() {
      const productList = this.data.productList;
      if(this.data._info.id){
        productList.splice(0,1);
      }
      this.setData({
          vals: [],
          productList,
          _info: {}
      })
     
    }
  },
lifetimes: {
    attached: function() {
      // 在组件实例进入页面节点树时执行
     
    },
    detached: function() {
      // 在组件实例被从页面节点树移除时执行
    },
}
})